package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.anno.LogOperation;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 16778
 * @date 2025/10/7 11:51
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userSerivce;

    @GetMapping
    public Result getUsers(UserDTO userDTO){
        PageResult<User> res = userSerivce.getUsers(userDTO);
        return Result.success(res);
    }

    @LogOperation
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("需要删除的用户id：{}",ids);
        userSerivce.delete(ids);
        return Result.success();
    }

    @LogOperation
    @PostMapping()
    public Result insert(@RequestBody User user){
        log.info("需要插入的用户数据：{}",user);
        userSerivce.insert(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查询的用户id:{}",id);
        User user = userSerivce.selectById(id);
        return Result.success(user);
    }

    @LogOperation
    @PutMapping
    public Result update(@RequestBody User user){
        log.info("需要更新的用户数据:{}",user);
        userSerivce.update(user);
        return Result.success();
    }

    @GetMapping("/list")
    public Result findAll(){
        List<User> list = userSerivce.findAll();
        return Result.success(list);
    }

    @GetMapping("/role/{roleLabel}")
    public Result selectByRoleLabel(@PathVariable String roleLabel){
        List<User> list = userSerivce.selectByRoleLabel(roleLabel);
        return Result.success(list);
    }

    @GetMapping("/dept/{deptId}")
    public Result selectByDeptId(@PathVariable Integer deptId){
        List<User> list = userSerivce.selectByDeptId(deptId);
        return Result.success(list);
    }

}
